package com.jfinal.aceadmin.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jfinal.aceadmin.vo.AttachmentVo;

public class JsonUtil {

	public static ObjectMapper objectMapper = new ObjectMapper();

	public static <T> List<T> parseObjectByJsonString(String attachmentString,
			Class<T> type) {
		List<T> responseCollection = new ArrayList<T>();
		attachmentString = "[" + attachmentString + "]";
		try {
			JsonNode jsonNode = objectMapper.readTree(attachmentString);
			Iterator<JsonNode> iterator = jsonNode.elements();
			while (iterator.hasNext()) {
				JsonNode node = iterator.next();
				responseCollection.add(objectMapper.readValue(node.toString(),
						type));
			}
			return responseCollection;
		} catch (Exception e) {
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		AttachmentVo attachmentVo = new AttachmentVo();
		attachmentVo.setFileName("test1.txt");
		attachmentVo.setFilePath("test1.txt");
		attachmentVo.setFileSize(1234);

		AttachmentVo attachmentVo2 = new AttachmentVo();
		attachmentVo2.setFileName("test2.txt");
		attachmentVo2.setFilePath("test2.txt");
		attachmentVo2.setFileSize(4567);

		StringBuilder sb = new StringBuilder();
		sb.append(objectMapper.writeValueAsString(attachmentVo)).append(",")
				.append(objectMapper.writeValueAsString(attachmentVo2));

		System.out.println(parseObjectByJsonString(sb.toString(),
				AttachmentVo.class));
	}
}
